package com.example.weather;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends Activity
{
	DrawDayWeather mydraw2;
	TextView textview;
	ArrayList<DayWeatherData> dayweather = new ArrayList<>();
	ArrayList<MyBitmapClass> my_bitmap_list = new ArrayList<>();
	MyCity myCity;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		final float refresh_rate = getWindowManager().getDefaultDisplay().getRefreshRate();//获取系统的刷新频率
		Log.e("TAG", "onCreate: refresh_rate " + refresh_rate);

		new MyCitiesFile(getApplicationContext());//一定要有这句，这句主要是给 MyCitiesFile 里的静态对象赋初值
		mydraw2 = findViewById(R.id.mydraw2);
		mydraw2.setRefresh_rate(refresh_rate);
		textview = findViewById(R.id.city_name);
		if (!new MyCitiesFile(getApplicationContext()).add_cities() || MyCitiesFile.MyCitieslist.size() == 0)
		{
			//一个城市都没有，强制打开选择城市页面
			Intent intent = new Intent(MainActivity.this, ChoiceCity.class);
			startActivityForResult(intent, 1);//打开城市选择界面
		}
		else
		{
			//已经有城市被加载出来
			myCity = MyCitiesFile.MyCitieslist.get(0);
			textview.setText(myCity.name);
			//Toast.makeText(getApplicationContext(), "拉取信息。。。" + myCity.name, Toast.LENGTH_SHORT).show();
			//new MyHttp().send_Get_String("https://weather.cma.cn/web/weather/" + myCity.number + ".html", handler1, 1);
			thread_get_data("https://weather.cma.cn/web/weather/" + myCity.number + ".html", handler1, 1);
			Log.i("TAG", "button_choice_city 1 3:" + "https://weather.cma.cn/web/weather/" + myCity.number + ".html" + "  " + MyCitiesFile.MyCitieslist.get(0).name);
		}
		//myCity=new MyCity();
		/*
		国内
		https://weather.cma.cn/api/dict/province
			市：北京（ABJ）
			https://weather.cma.cn/api/dict/province/ABJ
				区：北辰
				https://weather.cma.cn/web/weather/54528.html

		国外
		https://weather.cma.cn/api/dict/country
			国：东帝汶
			https://weather.cma.cn/api/dict/country/C115
				城市：帝力
				https://weather.cma.cn/web/weather/097390.html
		 */
		//new MyHttp().send_Get_String("https://weather.cma.cn/api/now/58606",handler1,1);
	}

	public void button_choice_city(View view)
	{
		Intent intent;
		Log.i("TAG", "button_choice_city 1:");
		intent = new Intent(MainActivity.this, ChoiceCity.class);
		Log.i("TAG", "button_choice_city 2:");
		startActivityForResult(intent, 0);//打开城市选择界面
		Log.i("TAG", "button_choice_city 3:");
	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
	{
		Log.i("TAG", "button_choice_city 0:");
		if (requestCode == 0)
		{
			if (resultCode == 3)
			{
				//Log.e("TAG", "button_choice_city 1 1:");
				mydraw2.setYValues(dayweather);
				mydraw2.invalidate();
				assert data != null;
				myCity = new MyCity(data.getStringExtra("city_name"), data.getStringExtra("city_number"));
				textview.setText(myCity.name);

				//Log.e("TAG", "button_choice_city 1 2:");
				//new MyHttp().send_Get_String("https://weather.cma.cn/web/weather/" + myCity.number + ".html", handler1, 1);
				thread_get_data("https://weather.cma.cn/web/weather/" + myCity.number + ".html", handler1, 1);
				//Toast.makeText(getApplicationContext(), "拉取信息。。。" + myCity.name, Toast.LENGTH_SHORT).show();
				Log.i("TAG", "button_choice_city 1 3:" + "https://weather.cma.cn/web/weather/" + myCity.number + ".html");
			}
		}
		else if (requestCode == 1)
		{
			if (resultCode == 3)
			{
				mydraw2.setYValues(dayweather);
				mydraw2.invalidate();
				assert data != null;
				myCity = new MyCity(data.getStringExtra("city_name"), data.getStringExtra("city_number"));
				textview.setText(myCity.name);
				//new MyHttp().send_Get_String("https://weather.cma.cn/web/weather/" + myCity.number + ".html", handler1, 1);
				thread_get_data("https://weather.cma.cn/web/weather/" + myCity.number + ".html", handler1, 1);
				Toast.makeText(getApplicationContext(), "拉取信息。。。" + myCity.name, Toast.LENGTH_SHORT).show();
				Log.i("TAG", "button_choice_city 1 3:" + "https://weather.cma.cn/web/weather/" + myCity.number + ".html");
			}
			else
			{
				Log.e("TAG", "onActivityResult:  退出了！！");
				finish();
				System.exit(0);
			}
		}
		//Log.i("TAG", "button_choice_city end:");
		super.onActivityResult(requestCode, resultCode, data);
	}

	@SuppressLint("HandlerLeak")
	final Handler handler1 = new Handler()
	{
		@Override
		public void handleMessage(@NonNull Message msg)
		{
			//			if (msg.what < 0)
			//			{
			//				Toast.makeText(getApplicationContext(), "拉取天气信息失败，正在重新拉取", Toast.LENGTH_LONG).show();
			//				new MyHttp().send_Get_String("https://weather.cma.cn/web/weather/" + myCity.number + ".html", handler1, 1);
			//				Log.e("TAG", "button_choice_city 拉取信息失败，重新拉取中。。。:" + "https://weather.cma.cn/web/weather/" + myCity.number + ".html");
			//			}
			Log.i("TAG", "handleMessage handler1:");
			if (msg.what == 1)
			{
				Log.i("TAG", "handleMessage handler1: 0");
				mydraw2.setYValues(dayweather);
				mydraw2.invalidate();
				Log.i("TAG", "handleMessage handler1: 1");
			}
			else if (msg.what == -1)
			{
				Toast.makeText(getApplicationContext(), "好像没有网络哦_3S后退出", Toast.LENGTH_LONG).show();
				//没有网，3s后自动推出，等有网之后由用户重启刷新界面
				new Thread()//开启请求发送邮件的线程
				{
					@Override
					public void run()
					{
						try
						{
							Thread.sleep(3000);
						} catch (InterruptedException e)
						{
							Log.e("TAG", "handler1 run: " + e.getMessage());
						}
						finish();
						System.exit(1);
					}
				}.start();
			}
			//Toast.makeText(getApplicationContext(), "刷新  " + myCity.name, Toast.LENGTH_SHORT).show();
		}
	};


	void data_cut(String str_data)
	{
		String[] str1 = new String[2];//0 储存这周的每日粗略数据  1 储存这周每日的详细信息
		str1[0] = str_data;
		Pattern p1 = Pattern.compile("class=\"row mt15\"([\\s\\S]+)id=\"myChart\"");//找到所有的天气数据
		Pattern p4 = Pattern.compile("<div[\\s]*class=\"high\">[\\s]*([\\d.\\-]+)[\\s]*℃[\\s]*</div>");//找到最高气温
		Pattern p5 = Pattern.compile("<div[\\s]*class=\"low\">[\\s]*([\\d.\\-]+)[\\s]*℃[\\s]*<");//最低气温
		Pattern p6 = Pattern.compile("<div[\\s]*class=\"day-item\">[\\s]*([^\\s]+)[\\s]*<br>([\\d]+/[\\d]+)</div>");//获取星期号和日期
		Pattern p7 = Pattern.compile("<img src=\"/static/img/w/icon/(w\\d+.png)\">");//获取图片的名称
		//Pattern p8 = Pattern.compile("");//分割tr
		//Pattern p6 = Pattern.compile("<div[\\s]*class=\"day-item\">[\\s]*([^\\s]+)[\\s]</div>");//其他条目
		//"pull-left day" 用这个分开一周每天的信息
		//Pattern p9 = Pattern.compile("<div[\\s]*class=\"low\">[\\s]*([^\\s]+)[\\s]</div>");
		//Pattern p10 = Pattern.compile("<div[\\s]*class=\"low\">[\\s]*([^\\s]+)[\\s]</div>");

		Pattern pp1 = Pattern.compile("<td>(\\d+:\\d+)</td>");//获取time
		Pattern pp2 = Pattern.compile("<td[\\s]*class=\"wicon\"><img src=\"/static/img/w/icon/(w\\d+.png)\"></td>");//获取图片的名字
		Pattern pp3 = Pattern.compile("<td>([\\d.-]+)℃</td>");//获取温度
		Pattern pp4 = Pattern.compile("<td>([^<>m]+)(m{0,2})</td>");//获取降水
		Pattern pp5 = Pattern.compile("<td>([\\d.-]*)([^<>]*)</td>");//获取风速
		Pattern pp6 = Pattern.compile("<td>([^<>]+)</td>");//获取风向
		Pattern pp7 = Pattern.compile("<td>([\\d.]*)[^<>]*</td>");//获取气压
		Pattern pp8 = Pattern.compile("<td>([\\d.-]+)%</td>");//获取湿度
		Pattern pp9 = Pattern.compile("<td>([\\d.-]+)%</td>");//获取云量


		//Log.i("TAG", "handleMessage: ");
		Matcher m = p1.matcher(str1[0]);
		Matcher m1;
		Matcher m2;
		Matcher m3;
		Matcher m4;
		if (m.find())
		{
			str1[0] = m.group(0);
			str1 = str1[0].split("class=\"mt15\"");//分开 日 天气数据和 天 天气数据
			//str1[0]
			dayweather = new ArrayList<>();
			for (String str_day : str1[0].split("pull-left day"))
			{
				//Log.i("TAG", "handleMessage: " + str_day);
				if (dayweather.size() == 7)
				{
					break;
				}
				m1 = p4.matcher(str_day);
				m2 = p5.matcher(str_day);
				m3 = p6.matcher(str_day);
				m4 = p7.matcher(str_day);
				if (m1.find() && m2.find() && m3.find() && m4.find())
				{
					dayweather.add(new DayWeatherData());
					dayweather.get(dayweather.size() - 1).day_high = m1.group(1);
					dayweather.get(dayweather.size() - 1).day_low = m2.group(1);
					dayweather.get(dayweather.size() - 1).day_name_week = m3.group(1);
					dayweather.get(dayweather.size() - 1).day_name_number = m3.group(2);
					dayweather.get(dayweather.size() - 1).weather_img[0] = m4.group(1);
					if (m4.find())
					{
						dayweather.get(dayweather.size() - 1).weather_img[1] = m4.group(1);
					}
					//						Log.i("TAG", "handleMessage: " + dayweather.get(dayweather.size() - 1).day_high);
					//						Log.i("TAG", "handleMessage: " + dayweather.get(dayweather.size() - 1).day_low);
					//						Log.i("TAG", "handleMessage: " + dayweather.get(dayweather.size() - 1).day_name_week);
					//						Log.i("TAG", "handleMessage: " + dayweather.get(dayweather.size() - 1).day_name_number);
					//Log.i("TAG", "handleMessage: " + dayweather.get(dayweather.size() - 1).weather_img[1]);
					//Log.i("TAG", "handleMessage: " + dayweather.get(dayweather.size() - 1).weather_img[0]);
				}
			}

			//str1[1]
			int week_day_id = 0;//天在数组中的索引
			final String[] STR_NAME = {"icon-time", "icon-tianqi", "icon-temp", "icon-raindrops", "icon-Windpower", "icon-fengxiang", "icon-qiya", "icon-humidity", "icon-yunliang_huabanfuben"};
			for (String hour_data_str : str1[1].split("<table class=\"hour-table\" id=\"hourTable_[0-9]+\"[\\s]*"))//把一周的七天拆开
			{
				//Log.i("TAG", "handleMessage: " + hour_data_str);
				if (hour_data_str.startsWith("style"))
				{
					DayWeatherData dayweather1 = dayweather.get(week_day_id);
					for (String hour_data_item : hour_data_str.split("[\\s]*<tr>[\\s]*"))//一天里的某一项所有时间的数据//处理一天里的时间段数据
					{

						if (hour_data_item.startsWith("<td"))//一天里某一项一个时间的数据
						{
							//Log.i("TAG", "handleMessage  hour_data_item: " + hour_data_item);
							for (int k = 0; k < STR_NAME.length; k++)//数据项的名称索引id
							{
								if (hour_data_item.lastIndexOf(STR_NAME[k]) > 0)
								{
									//hour_data_item.split("")

									int kk = 0;//数据项对应的时间索引id
									switch (k)
									{
										case 0:
											//Log.i("TAG", "data_cut  hour_data_item: "+hour_data_item);
											//Log.i("TAG", "data_cut  week_day_id: 1");
											m1 = pp1.matcher(hour_data_item);
											//Log.i("TAG", "data_cut  week_day_id:2 ");
											while (m1.find())
											{
												//Log.i("TAG", "data_cut  week_day_id: 3");
												dayweather1.timeWeatherDatas.add(new TimeWeatherData());
												//Log.i("TAG", "data_cut  week_day_id: 4");
												dayweather1.timeWeatherDatas.get(kk).time = m1.group(1);
												//Log.i("TAG", "data_cut  week_day_id: 5");
												//Log.e("TAG", "data_cut   " + week_day_id + "  dayweather1.timeWeatherDatas.get(" + k + ").time: " + dayweather1.timeWeatherDatas.get(k).time);
												kk++;//
												//Log.i("TAG", "data_cut  week_day_id: 6");
											}
											if (kk != dayweather1.timeWeatherDatas.size())
											{
												Log.e("TAG", "data_cut kk error : kk=" + kk + "  k=" + k);
											}
											break;
										case 1:
											//Log.i("TAG", "data_cut  hour_data_item: "+hour_data_item);
											//Log.i("TAG", "data_cut  week_day_id: "+week_day_id);
											m1 = pp2.matcher(hour_data_item);
											while (m1.find())
											{
												//dayweather1.timeWeatherDatas.add(new TimeWeatherData());
												if (kk > dayweather1.timeWeatherDatas.size())
												{
													//Log.e("TAG", "data_cut break1: " + kk);
													break;//超出会访问位置单元，会报错
												}
												dayweather1.timeWeatherDatas.get(kk).weather_img = m1.group(1);
												//Log.e("TAG", "data_cut   " + week_day_id + "  dayweather1.timeWeatherDatas.get(" + kk + ").time: " + dayweather1.timeWeatherDatas.get(kk).weather_img);
												kk++;//
											}
											if (kk != dayweather1.timeWeatherDatas.size())
											{
												Log.e("TAG", "data_cut kk error : kk=" + kk + "  k=" + k);
											}
											break;
										case 2:
											//Log.i("TAG", "data_cut  hour_data_item: "+hour_data_item);
											//Log.i("TAG", "data_cut  week_day_id: "+week_day_id);
											m1 = pp3.matcher(hour_data_item);
											while (m1.find())
											{
												if (kk > dayweather1.timeWeatherDatas.size())
												{
													//Log.e("TAG", "data_cut break1: " + kk);
													break;//超出会访问位置单元，会报错
												}
												if (m1.group(1) == null || m1.group(1).equals(""))
												{
													dayweather1.timeWeatherDatas.get(kk).temperature_str = null;
													dayweather1.timeWeatherDatas.get(kk).temperature_float = 100;
												}
												else
												{
													dayweather1.timeWeatherDatas.get(kk).temperature_str = m1.group(1);
													dayweather1.timeWeatherDatas.get(kk).temperature_float = Float.parseFloat(m1.group(1));
												}
												//Log.e("TAG", "data_cut   " + week_day_id + "  dayweather1.timeWeatherDatas.get(" + kk + ").time: " + dayweather1.timeWeatherDatas.get(kk).weather_img);
												kk++;//
											}
											if (kk != dayweather1.timeWeatherDatas.size())
											{
												Log.e("TAG", "data_cut kk error : kk=" + kk + "  k=" + k);
											}
											break;
										case 3:
											m1 = pp4.matcher(hour_data_item);
											while (m1.find())
											{
												if (kk > dayweather1.timeWeatherDatas.size())
												{
													//Log.e("TAG", "data_cut break1: " + kk);
													break;//超出会访问位置单元，会报错
												}
												dayweather1.timeWeatherDatas.get(kk).jiang_shui_liang = m1.group(1);
												if (m1.group(1).equals("无降水"))
												{
													dayweather1.timeWeatherDatas.get(kk).jiang_shui_liang_float = 0;
												}
												else
												{
													dayweather1.timeWeatherDatas.get(kk).jiang_shui_liang_float = Float.parseFloat(m1.group(1));
												}
												kk++;//
											}
											if (kk != dayweather1.timeWeatherDatas.size())
											{
												Log.e("TAG", "data_cut kk error : kk=" + kk + "  k=" + k);
											}
											break;
										case 4:
											m1 = pp5.matcher(hour_data_item);
											while (m1.find())
											{
												if (kk > dayweather1.timeWeatherDatas.size())
												{
													//Log.e("TAG", "data_cut break1: " + kk);
													break;//超出会访问位置单元，会报错
												}
												if (m1.group(1) == null || m1.group(1).equals(""))
												{
													dayweather1.timeWeatherDatas.get(kk).feng_su = m1.group(2);
													dayweather1.timeWeatherDatas.get(kk).feng_su_float = 0;
												}
												else
												{
													dayweather1.timeWeatherDatas.get(kk).feng_su = m1.group(1);
													dayweather1.timeWeatherDatas.get(kk).feng_su_float = Float.parseFloat(m1.group(1));
												}
												kk++;//
											}
											if (kk != dayweather1.timeWeatherDatas.size())
											{
												Log.e("TAG", "data_cut kk error : kk=" + kk + "  k=" + k);
											}
											break;
										case 5:
											m1 = pp6.matcher(hour_data_item);
											while (m1.find())
											{
												if (kk > dayweather1.timeWeatherDatas.size())
												{
													//Log.e("TAG", "data_cut break1: " + kk);
													break;//超出会访问位置单元，会报错
												}
												dayweather1.timeWeatherDatas.get(kk).feng_xiang = m1.group(1);
												kk++;//
											}
											if (kk != dayweather1.timeWeatherDatas.size())
											{
												Log.e("TAG", "data_cut kk error : kk=" + kk + "  k=" + k);
											}
											break;
										case 6:
											m1 = pp7.matcher(hour_data_item);
											while (m1.find())
											{
												if (kk > dayweather1.timeWeatherDatas.size())
												{
													//Log.e("TAG", "data_cut break1: " + kk);
													break;//超出会访问位置单元，会报错
												}
												if (m1.group(1) == null || m1.group(1).equals(""))
												{
													dayweather1.timeWeatherDatas.get(kk).qi_ya = "";
												}
												else
												{
													dayweather1.timeWeatherDatas.get(kk).qi_ya = m1.group(1);
													dayweather1.timeWeatherDatas.get(kk).qi_ya_int = Float.parseFloat(m1.group(1));
												}
												kk++;//
											}
											if (kk != dayweather1.timeWeatherDatas.size())
											{
												Log.e("TAG", "data_cut kk error : kk=" + kk + "  k=" + k);
											}
											break;
										case 7:
											m1 = pp8.matcher(hour_data_item);
											while (m1.find())
											{
												if (kk > dayweather1.timeWeatherDatas.size())
												{
													//Log.e("TAG", "data_cut break1: " + kk);
													break;//超出会访问位置单元，会报错
												}
												dayweather1.timeWeatherDatas.get(kk).humidity = m1.group(1);
												dayweather1.timeWeatherDatas.get(kk).humidity_int = Float.parseFloat(m1.group(1));
												kk++;//
											}
											if (kk != dayweather1.timeWeatherDatas.size())
											{
												Log.e("TAG", "data_cut kk error : kk=" + kk + "  k=" + k);
											}
											break;
										case 8:
											m1 = pp9.matcher(hour_data_item);
											while (m1.find())
											{
												if (kk > dayweather1.timeWeatherDatas.size())
												{
													//Log.e("TAG", "data_cut break1: " + kk);
													break;//超出会访问位置单元，会报错
												}
												dayweather1.timeWeatherDatas.get(kk).yun_liang = m1.group(1);
												//Log.e("TAG", "data_cut: " + m1.group(1));
												dayweather1.timeWeatherDatas.get(kk).yun_liang_float = Float.parseFloat(m1.group(1));
												kk++;//
											}
											if (kk != dayweather1.timeWeatherDatas.size())
											{
												Log.e("TAG", "data_cut kk error : kk=" + kk + "  k=" + k);
											}
											break;
									}
									break;
								}
							}
						}
					}
//						//输出当前天的各个时间段信息
//					for (TimeWeatherData str11 : dayweather1.timeWeatherDatas)
//					{
//						Log.i("TAG", "data_cut  istr11.time: " + str11.time);
//						Log.i("TAG", "data_cut  istr11.weather_img: " + str11.weather_img);
//						Log.i("TAG", "data_cut  istr11.temperature_str: " + str11.temperature_str);
//						Log.i("TAG", "data_cut  istr11.jiang_shui_liang: " + str11.jiang_shui_liang);
//						Log.i("TAG", "data_cut  istr11.feng_su: " + str11.feng_su);
//						Log.i("TAG", "data_cut  istr11.feng_xiang: " + str11.feng_xiang);
//						Log.i("TAG", "data_cut  istr11.qi_ya: " + str11.qi_ya);
//						Log.i("TAG", "data_cut  istr11.humidity: " + str11.humidity);
//						Log.i("TAG", "data_cut  istr11.yun_liang: " + str11.yun_liang);
//					}

					week_day_id++;
//					if (week_day_id == 5)
//					{
//						//小时数据保留两天的就好了，保留的太多预测不准或者输出太多。。。
//						break;
//					}
//					else
//					{
//						Log.i("TAG", "data_cut  week_day_id: " + week_day_id);
//					}
				}
//				else
//				{
//					Log.i("TAG", "handleMessage: " + hour_data_str);
//				}
			}
		}
	}


	void thread_get_data(final String net_url, final Handler handler, final int CHANGE_UI)
	{
		new Thread()
		{
			@Override
			public void run()
			{
				//super.run();

				String str1 = null;
				str1 = new MyHttp().send_Get_String(net_url);
				int i = 3;
				while (i > 0 && str1 == null)
				{
					str1 = new MyHttp().send_Get_String(net_url);
					i--;
				}
				Message msg;
				if (i <= 0)
				{
					msg = new Message();
					msg.what = -1;
					//Log.i("TAG", "run: 请求"+ result);
					//msg.obj = result.toString();
					handler.sendMessage(msg);
					return;
				}
				data_cut(str1);//执行完这个函数就可以绘制温度折线了，但是还没有获取到当日的天气图片
				msg = new Message();
				msg.what = CHANGE_UI;
				//Log.i("TAG", "run: 请求"+ result);
				//msg.obj = result.toString();
				handler.sendMessage(msg);
				File file;
				Bitmap bitmap;
				//从网页或者内存里加载图片
				for (int k = 0; k < dayweather.size(); k++)
				{

					for (TimeWeatherData timeWeatherData_X : dayweather.get(k).timeWeatherDatas)
					{
						timeWeatherData_X.bitmap_img = null;
						for (int a = 0; a < my_bitmap_list.size(); a++)
						{
							if (my_bitmap_list.get(a).name.equals(timeWeatherData_X.weather_img))
							{
								timeWeatherData_X.bitmap_img = my_bitmap_list.get(a).bitmap;
								break;
							}
						}
						if (timeWeatherData_X.bitmap_img != null)
						{
							continue;
						}
						file = new File(getApplicationContext().getFilesDir() + "/" + timeWeatherData_X.weather_img);
						if (file.exists())
						{
							//如果存在就加载文件里的图片
							try
							{
								FileInputStream stream = new FileInputStream(getApplicationContext().getFilesDir() + "/" + timeWeatherData_X.weather_img);
								bitmap = BitmapFactory.decodeStream(stream);
								stream.close();
								my_bitmap_list.add(new MyBitmapClass(timeWeatherData_X.weather_img, bitmap));
								timeWeatherData_X.bitmap_img = bitmap;
							} catch (FileNotFoundException e)
							{
								Log.e("TAG", "从文件中加载图片失败" + e.getMessage());
							} catch (IOException e)
							{
								Log.e("TAG", "从文件中加载图片失败" + e.getMessage());
							}
						}
						else
						{
							bitmap = new MyHttp().send_Get_Bitmap("https://weather.cma.cn/static/img/w/icon/" + timeWeatherData_X.weather_img);
							if (bitmap == null)
							{
								Log.e("TAG", "run  请求图片失败00: " + "https://weather.cma.cn/static/img/w/icon/" + timeWeatherData_X.weather_img);
							}
							//请求失败之后不直接退出，去文件里面看看有没有其他线程已经请求成功
							try
							{
								FileOutputStream outputStream = new FileOutputStream(getApplicationContext().getFilesDir() + "/" + timeWeatherData_X.weather_img);
								my_bitmap_list.add(new MyBitmapClass(timeWeatherData_X.weather_img, bitmap));
								timeWeatherData_X.bitmap_img = bitmap;
								bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
								outputStream.flush();
								outputStream.close();
							} catch (FileNotFoundException e)
							{
								//e.printStackTrace();
								Log.e("TAG", "网站下载之后保存图片失败11" + e.getMessage());
							} catch (IOException e)
							{
								//e.printStackTrace();
								Log.e("TAG", "网站下载之后保存图片失败22" + e.getMessage());
							}
							//不存在就从网页获取之后再加载。
						}
					}


					for (int b = 0; b < 2; b++)
					{
						//先从 my_bitmap_list 数组里找同名图片，然后再从文件或者网站下载
						dayweather.get(k).bitmap[b] = null;
						for (int a = 0; a < my_bitmap_list.size(); a++)
						{
							if (my_bitmap_list.get(a).name.equals(dayweather.get(k).weather_img[b]))
							{
								dayweather.get(k).bitmap[b] = my_bitmap_list.get(a).bitmap;
								break;
							}
						}
						if (dayweather.get(k).bitmap[b] != null)
						{
							continue;
						}
						//从文件中加载
						file = new File(getApplicationContext().getFilesDir() + "/" + dayweather.get(k).weather_img[b]);
						if (file.exists())
						{
							//如果存在就加载文件里的图片
							try
							{
								FileInputStream stream = new FileInputStream(getApplicationContext().getFilesDir() + "/" + dayweather.get(k).weather_img[b]);
								bitmap = BitmapFactory.decodeStream(stream);
								stream.close();
								my_bitmap_list.add(new MyBitmapClass(dayweather.get(k).weather_img[b], bitmap));
								dayweather.get(k).bitmap[b] = bitmap;

							} catch (FileNotFoundException e)
							{
								Log.e("TAG", "从文件中加载图片失败" + e.getMessage());
							} catch (IOException e)
							{
								Log.e("TAG", "从文件中加载图片失败" + e.getMessage());
							}
						}
						else
						{
							bitmap = new MyHttp().send_Get_Bitmap("https://weather.cma.cn/static/img/w/icon/" + dayweather.get(k).weather_img[b]);
							if (bitmap == null)
							{
								Log.e("TAG", "run  请求图片失败: " + "https://weather.cma.cn/static/img/w/icon/" + dayweather.get(k).weather_img[b]);
								break;
							}
							try
							{
								FileOutputStream outputStream = new FileOutputStream(getApplicationContext().getFilesDir() + "/" + dayweather.get(k).weather_img[b]);
								my_bitmap_list.add(new MyBitmapClass(dayweather.get(k).weather_img[b], bitmap));
								dayweather.get(k).bitmap[b] = bitmap;
								bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
								outputStream.flush();
								outputStream.close();
							} catch (FileNotFoundException e)
							{
								//e.printStackTrace();
								Log.e("TAG", "网站下载之后保存图片失败1" + e.getMessage());
							} catch (IOException e)
							{
								//e.printStackTrace();
								Log.e("TAG", "网站下载之后保存图片失败2" + e.getMessage());
							}
							//不存在就从网页获取之后再加载。
						}

					}
					//"https://weather.cma.cn/static/img/w/icon/"+dayweather.get(k).weather_img[0]

				}

				msg = new Message();
				msg.what = CHANGE_UI;
				//Log.i("TAG", "run: 请求"+ result);
				//msg.obj = result.toString();
				handler.sendMessage(msg);
				Log.e("TAG", "图片请求结束++++++++++++++++++++++++++++++++++++++");
			}
		}.start();
	}

	class MyBitmapClass
	{
		String name = null;
		Bitmap bitmap = null;

		public MyBitmapClass(String name, Bitmap bitmap)
		{
			this.name = name;
			this.bitmap = bitmap;
		}
	}
}
